
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Drawable · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.2">
        
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="item-template.html" />
    
    
    <link rel="prev" href="textview.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        <li class="header">----安卓 UI----</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="view.html">
            
                <a href="view.html">
            
                    
                    自定义控件和measure
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="canvas-paint.html">
            
                <a href="canvas-paint.html">
            
                    
                    draw和canvas，paint详解
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="anim.html">
            
                <a href="anim.html">
            
                    
                    动画
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="notify.html">
            
                <a href="notify.html">
            
                    
                    notify系列库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="loading.html">
            
                <a href="loading.html">
            
                    
                    loading系列库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="status-ui.html">
            
                <a href="status-ui.html">
            
                    
                    StatusUI系列库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="touch-scroll.html">
            
                <a href="touch-scroll.html">
            
                    
                    滑动
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="layout.html">
            
                <a href="layout.html">
            
                    
                    Layout
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="textview.html">
            
                <a href="textview.html">
            
                    
                    TextView
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.11" data-path="drawable.html">
            
                <a href="drawable.html">
            
                    
                    Drawable
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="item-template.html">
            
                <a href="item-template.html">
            
                    
                    列表Adapter模板化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="recyclerview.html">
            
                <a href="recyclerview.html">
            
                    
                    RecyclerView全解
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="design.html">
            
                <a href="design.html">
            
                    
                    MeterialDesign详解
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="component.html">
            
                <a href="component.html">
            
                    
                    AyoComponent基础库--Activity和Fragment
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="systembartint.html">
            
                <a href="systembartint.html">
            
                    
                    SystembarTint
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="permission-ask.html">
            
                <a href="permission-ask.html">
            
                    
                    权限控制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="keyboard.html">
            
                <a href="keyboard.html">
            
                    
                    小键盘
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19" data-path="notification.html">
            
                <a href="notification.html">
            
                    
                    Notification
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">----安卓 foundation----</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="mvp.html">
            
                <a href="mvp.html">
            
                    
                    MVP
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="rxjava.html">
            
                <a href="rxjava.html">
            
                    
                    RxJava
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="lang.html">
            
                <a href="lang.html">
            
                    
                    常用工具类
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="http.html">
            
                <a href="http.html">
            
                    
                    http
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="xml.html">
            
                <a href="xml.html">
            
                    
                    xml解析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="json-reporter.html">
            
                <a href="json-reporter.html">
            
                    
                    Json解析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="inject.html">
            
                <a href="inject.html">
            
                    
                    ButterKnife
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="thread.html">
            
                <a href="thread.html">
            
                    
                    安卓线程和looper
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.9" data-path="process.html">
            
                <a href="process.html">
            
                    
                    多进程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.10" data-path="eventbus.html">
            
                <a href="eventbus.html">
            
                    
                    EventBus
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.11" data-path="db.html">
            
                <a href="db.html">
            
                    
                    数据库和ContentProvider
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.12" data-path="job.html">
            
                <a href="job.html">
            
                    
                    定时任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.13" data-path="service.html">
            
                <a href="service.html">
            
                    
                    service
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.14" data-path="opt-mem.html">
            
                <a href="opt-mem.html">
            
                    
                    内存优化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.15" data-path="opt-network.html">
            
                <a href="opt-network.html">
            
                    
                    网络优化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.16" data-path="opt-anr.html">
            
                <a href="opt-anr.html">
            
                    
                    anr排查
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.17" data-path="opt-ui.html">
            
                <a href="opt-ui.html">
            
                    
                    UI卡顿优化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.18" data-path="opt-launch.html">
            
                <a href="opt-launch.html">
            
                    
                    启动优化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.19" data-path="opt-launch.html">
            
                <a href="opt-launch.html">
            
                    
                    混淆
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.20" data-path="hotfix.html">
            
                <a href="hotfix.html">
            
                    
                    热补丁混淆
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.21" data-path="aar.html">
            
                <a href="aar.html">
            
                    
                    aar相关
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.22" data-path="route.html">
            
                <a href="route.html">
            
                    
                    Route路由
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.23" data-path="modules.html">
            
                <a href="modules.html">
            
                    
                    分模块，组件化，插件化
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">----Gradle----</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="gradle.html">
            
                <a href="gradle.html">
            
                    
                    gradle基本配置
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="gradle-plugin.html">
            
                <a href="gradle-plugin.html">
            
                    
                    gradle插件开发
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">----工具----</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="repos.html">
            
                <a href="repos.html">
            
                    
                    优秀博客，开源项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="packs.html">
            
                <a href="packs.html">
            
                    
                    自动打包，多渠道打包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="gist.html">
            
                <a href="gist.html">
            
                    
                    代码片段和常见问题
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="stetho.html">
            
                <a href="stetho.html">
            
                    
                    stetho
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="fabric.html">
            
                <a href="fabric.html">
            
                    
                    fabric
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.6" data-path="app-update.html">
            
                <a href="app-update.html">
            
                    
                    自动升级
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.7" data-path="bugsnags.html">
            
                <a href="bugsnags.html">
            
                    
                    日志和崩溃统计
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.8" data-path="mail.html">
            
                <a href="mail.html">
            
                    
                    发邮件辅助接口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.9" data-path="socail.html">
            
                <a href="socail.html">
            
                    
                    第三方登录和分享
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.10" data-path="pay-alipay.html">
            
                <a href="pay-alipay.html">
            
                    
                    支付宝支付
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.11" data-path="pay-wx.html">
            
                <a href="pay-wx.html">
            
                    
                    微信支付
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.12" data-path="umeng.html">
            
                <a href="umeng.html">
            
                    
                    友盟统计
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.13" data-path="push-jpush.html">
            
                <a href="push-jpush.html">
            
                    
                    推送-极光
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.14" data-path="push-xiaomi.html">
            
                <a href="push-xiaomi.html">
            
                    
                    推送-小米
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >Drawable</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="drawable">Drawable</h1>
<h1 id="drawable">Drawable</h1>
<ul>
<li>&#x672C;&#x6587;&#x63A2;&#x8BA8;&#x7684;&#x662F;&#xFF1A;<ul>
<li>&#x5B89;&#x5353;&#x63D0;&#x4F9B;&#x7684;&#x51E0;&#x4E2A;Drawable&#x7C7B;&#x578B;</li>
<li>&#x81EA;&#x5B9A;&#x4E49;Drawable</li>
<li>Drawable&#x7684;&#x4F7F;&#x7528;&#x573A;&#x666F;</li>
</ul>
</li>
</ul>
<ul>
<li>&#x524D;&#x8A00;<ul>
<li>&#x6CE8;&#x610F;Drawable&#x7528;&#x5728;&#x54EA;&#x513F;&#xFF0C;&#x4E00;&#x822C;&#x80FD;&#x8BBE;&#x7F6E;background&#x548C;src&#x7684;&#x5730;&#x65B9;&#x90FD;&#x80FD;&#x8BBE;&#x7F6E;Drawable&#xFF0C;&#x5C31;&#x662F;R.drawable</li>
</ul>
</li>
</ul>
<h2 id="1-&#x5185;&#x7F6E;drawable&#x7C7B;&#x578B;">1 &#x5185;&#x7F6E;Drawable&#x7C7B;&#x578B;</h2>
<h3 id="bitmapdrawable&#x548C;nineoldpatchdrawable&#xFF1A;&#x4F4D;&#x56FE;">BitmapDrawable&#x548C;NineOldPatchDrawable&#xFF1A;&#x4F4D;&#x56FE;</h3>
<p>&#x5BF9;&#x5E94;&#x7684;xml&#xFF1A;</p>
<pre><code class="lang-java">&lt;bitmap xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    android:src=&quot;@drawable/line&quot;
    android:tileMode=&quot;repeat&quot;
    android:antialias=&quot;&quot;
    android:filter=&quot;&quot;
    android:dither=&quot;&quot;
    android:gravity=&quot;&quot;
    &gt;
&lt;/bitmap&gt;
</code></pre>
<ul>
<li><p>&#x4ECB;&#x7ECD;&#xFF1A;</p>
<ul>
<li>&#x4E00;&#x4E2A;BitmapDrawable&#x5C31;&#x662F;&#x5C01;&#x88C5;&#x4E86;&#x4E00;&#x4E2A;&#x4F4D;&#x56FE;</li>
<li>&#x76F4;&#x63A5;&#x4EE5;&#x6587;&#x4EF6;&#x7684;&#x65B9;&#x5F0F;&#xFF0C;&#x5C31;&#x662F;&#x5C01;&#x88C5;&#x4E86;&#x4E00;&#x4E2A;&#x539F;&#x59CB;&#x7684;&#x4F4D;&#x56FE;</li>
<li>&#x4EE5;Xml&#x65B9;&#x5F0F;&#xFF0C;&#x53EF;&#x4EE5;&#x5BF9;&#x539F;&#x59CB;&#x7684;&#x4F4D;&#x56FE;&#x8FDB;&#x884C;&#x4E00;&#x7CFB;&#x5217;&#x7684;&#x5904;&#x7406;&#xFF0C;&#x6BD4;&#x5982;&#x8BF4;&#x6297;&#x952F;&#x9F7F;&#xFF0C;&#x62C9;&#x4F38;&#xFF0C;&#x5BF9;&#x9F50;&#x7B49;&#x7B49;&#x3002;</li>
<li>&#x8981;&#x4E86;&#x89E3;BitmapDrawable&#x7684;&#x4F7F;&#x7528;&#xFF0C;&#x8FD8;&#x9700;&#x8981;&#x660E;&#x767D;Bitmap&#x3001;BitmapFactory&#x7B49;&#x7C7B;<ul>
<li>Bitmap&#x4EE3;&#x8868;&#x4E86;&#x4E00;&#x4E2A;&#x539F;&#x59CB;&#x7684;&#x4F4D;&#x56FE;&#xFF0C;&#x5E76;&#x4E14;&#x53EF;&#x4EE5;&#x5BF9;&#x4F4D;&#x56FE;&#x8FDB;&#x884C;&#x4E00;&#x7CFB;&#x5217;&#x7684;&#x53D8;&#x6362;&#x64CD;&#x4F5C;</li>
<li>BitmapFactory&#x63D0;&#x4F9B;&#x4E00;&#x7CFB;&#x5217;&#x7684;&#x65B9;&#x6CD5;&#x7528;&#x4E8E;&#x4EA7;&#x751F;&#x4E00;&#x4E2A;Bitmap&#x5BF9;&#x8C61;&#xFF0C; &#x591A;&#x7528;&#x5728;Canvas&#x4E2D;</li>
</ul>
</li>
</ul>
</li>
<li><p>Tile&#x6A21;&#x5F0F;&#xFF1A;</p>
<ul>
<li>&#x4EE3;&#x7801;&#x8BBE;&#x7F6E;&#xFF1A;<code>drawable2.setTileModeXY(TileMode.REPEAT, TileMode.CLAMP)</code></li>
<li>&#x5F53;tile&#x6A21;&#x5F0F;&#x88AB;&#x542F;&#x7528;&#xFF0C;&#x4F4D;&#x56FE;&#x662F;&#x91CD;&#x590D;&#x7684;&#xFF0C;&#x5E76;&#x4E14;gravity&#x5C5E;&#x6027;&#x5C06;&#x88AB;&#x5FFD;&#x7565;</li>
<li>REPEAT&#xFF1A;&#x91CD;&#x590D;&#xFF0C;&#x53EF;&#x4EE5;&#x7528;&#x6765;&#x753B;&#x865A;&#x7EBF;&#xFF0C;&#x9700;&#x63D0;&#x4F9B;&#x4E00;&#x5F20;&#x77ED;&#x7684;&#x865A;&#x7EBF;&#x56FE;</li>
<li>MIRROR&#xFF1A;&#x955C;&#x9762;&#x53CD;&#x5C04;</li>
<li>CLAMP&#xFF1A;&#x8FB9;&#x7F18;&#x62C9;&#x4F38;&#xFF0C;&#x4E0D;&#x597D;&#x770B;&#xFF0C;&#x4E0D;&#x77E5;&#x9053;&#x4EC0;&#x4E48;&#x65F6;&#x5019;&#x53EF;&#x4EE5;&#x7528;&#xFF0C;&#x8FD9;&#x4E2A;&#x548C;&#x7535;&#x8111;&#x5C4F;&#x4FDD;&#x7684;&#x6A21;&#x5F0F;&#x5E94;&#x8BE5;&#x6709;&#x4E9B;&#x4E0D;&#x540C;&#xFF0C;&#x8FD9;&#x4E2A;&#x62C9;&#x4F38;&#x7684;&#x662F;&#x56FE;&#x7247;&#x6700;&#x540E;&#x7684;&#x90A3;&#x4E00;&#x4E2A;&#x50CF;&#x7D20;&#xFF1B;&#x6A2A;&#x5411;&#x7684;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x6A2A;&#x884C;&#x50CF;&#x7D20;&#xFF0C;&#x4E0D;&#x65AD;&#x7684;&#x91CD;&#x590D;&#xFF0C;&#x7EB5;&#x9879;&#x7684;&#x90A3;&#x4E00;&#x5217;&#x50CF;&#x7D20;&#xFF0C;&#x4E0D;&#x65AD;&#x7684;&#x91CD;&#x590D;</li>
</ul>
</li>
</ul>
<p><strong>&#x901A;&#x8FC7;&#x4EE3;&#x7801;&#x63A7;&#x5236;</strong></p>
<pre><code class="lang-java"><span class="hljs-comment">//&#x65B9;&#x5F0F;1</span>
InputStream inputStream = getResources().openRawResource(R.drawable.animate_shower);
BitmapDrawable drawable = <span class="hljs-keyword">new</span> BitmapDrawable(inputStream);
Bitmap bitmap = drawable.getBitmap();

<span class="hljs-comment">//&#x65B9;&#x5F0F;2</span>
Resources res = getResources();
BitmapDrawable drawable2 = (BitmapDrawable) res.getDrawable(R.drawable.animate_shower);

<span class="hljs-comment">//&#x83B7;&#x53D6;</span>
drawable2.getCurrent();<span class="hljs-comment">//&#x5BF9;&#x4E8E;&#x6CA1;&#x6709;&#x72B6;&#x6001;&#x53D8;&#x5316;&#x7684;Drawable&#xFF0C;&#x8FD9;&#x4E2A;&#x5C31;&#x662F;drawable.this&#xFF0C;&#x5BF9;&#x4E8E;&#x9009;&#x62E9;&#x5668;&#x6216;&#x8005;level drawable&#xFF0C;&#x8FD9;&#x4E2A;&#x5C31;&#x662F;&#x5F53;&#x524D;&#x88AB;&#x6FC0;&#x6D3B;&#x7684;drawable&#xFF0C;&#x56E0;&#x4E3A;&#x662F;&#x591A;&#x4E2A;drawable&#x7684;&#x7EC4;&#x5408;</span>


<span class="hljs-comment">//&#x5E38;&#x89C1;&#x5C5E;&#x6027;</span>
drawable2.setTileModeXY(TileMode.REPEAT, TileMode.CLAMP);<span class="hljs-comment">//Sets the repeat behavior&#xFF0C;&#x5F53;Bitmap&#x5C0F;&#x4E8E;&#x8FD9;&#x4E2A;drawable&#x65F6;&#xFF0C;&#x9700;&#x8981;&#x6307;&#x5B9A;&#x91CD;&#x590D;&#x6A21;&#x5F0F;</span>
drawable2.setGravity(Gravity.CENTER);<span class="hljs-comment">//&#x8BBE;&#x7F6E;bitmap&#x5728;bound&#x4E2D;&#x7684;&#x4F4D;&#x7F6E;&#xFF0C;&#x548C;tile&#x6A21;&#x5F0F;&#x51B2;&#x7A81;&#xFF0C;&#x5373;&#x53EA;&#x6709;tile&#x4E0D;&#x662F;disable&#x65F6;&#xFF0C;gravity&#x624D;&#x8D77;&#x4F5C;&#x7528;&#xFF0C;&#x8981;&#x4E48;&#x91CD;&#x590D;&#xFF0C;&#x8981;&#x4E48;&#x8003;&#x8651;gravity</span>
drawable2.setAntiAlias(<span class="hljs-keyword">true</span>);<span class="hljs-comment">//&#x6297;&#x952F;&#x9F7F;</span>
drawable2.setFilterBitmap(<span class="hljs-keyword">false</span>);<span class="hljs-comment">//&#x5927;&#x4F53;&#x4E0A;&#x5C31;&#x662F;&#x4F7F;&#x7528;Bitmap&#x65F6;&#xFF0C;&#x5982;&#x679C;&#x8981;&#x7F29;&#x653E;&#x6216;&#x65CB;&#x8F6C;&#xFF0C;&#x4F1A;&#x6539;&#x5584;&#x89C6;&#x89C9;&#x6548;&#x679C;&#xFF0C;&#x4F46;&#x901F;&#x5EA6;&#x53D8;&#x6162;</span>
drawable2.setDither(<span class="hljs-keyword">false</span>);<span class="hljs-comment">//&#x5982;&#x679C;&#x4F4D;&#x56FE;&#x4E0E;&#x5C4F;&#x5E55;&#x7684;&#x50CF;&#x7D20;&#x914D;&#x7F6E;&#x4E0D;&#x540C;&#x65F6;&#xFF0C;&#x662F;&#x5426;&#x5141;&#x8BB8;&#x6296;&#x52A8;.&#xFF08;&#x4F8B;&#x5982;&#xFF1A;&#x4E00;&#x4E2A;&#x4F4D;&#x56FE;&#x7684;&#x50CF;&#x7D20;&#x8BBE;&#x7F6E;&#x662F; ARGB 8888&#xFF0C;&#x4F46;&#x5C4F;&#x5E55;&#x7684;&#x8BBE;&#x7F6E;&#x662F;RGB 565&#xFF09;</span>
</code></pre>
<h3 id="clipdrawable-&#x88C1;&#x526A;">ClipDrawable: &#x88C1;&#x526A;</h3>
<p>&#x5BF9;&#x5E94;&#x7684;xml</p>
<pre><code class="lang-java">&lt;clip xmlns:android=<span class="hljs-string">&quot;http://schemas.android.com/apk/res/android&quot;</span>
    android:drawable=<span class="hljs-string">&quot;@drawable/android&quot;</span>
    android:clipOrientation=<span class="hljs-string">&quot;horizontal&quot;</span>
    android:gravity=<span class="hljs-string">&quot;left&quot;</span> /&gt;
</code></pre>
<p>&#x56FE;&#x7247;&#x88C1;&#x526A;&#xFF1A;</p>
<ul>
<li>&#x2014;&#x2014;&#x9ED8;&#x8BA4;&#x662F;0&#xFF0C;&#x5373;&#x5B8C;&#x5168;&#x88C1;&#x526A;</li>
<li>&#x2014;&#x2014;&#x6700;&#x5927;&#x662F;10000&#xFF0C;&#x5373;&#x5B8C;&#x5168;&#x4E0D;&#x88C1;&#x526A;</li>
<li>&#x2014;&#x2014;&#x901A;&#x8FC7;setLevel&#x8BBE;&#x7F6E;&#x88C1;&#x526A;&#x5927;&#x5C0F;</li>
</ul>
<ul>
<li><p>&#x91CD;&#x70B9;&#x5C5E;&#x6027;&#xFF1A;</p>
<ul>
<li><p>level&#xFF1A;</p>
<ul>
<li>&#x9ED8;&#x8BA4;&#x662F;0&#xFF0C;&#x5373;&#x5B8C;&#x5168;&#x88C1;&#x526A;</li>
<li>&#x6700;&#x5927;&#x662F;10000&#xFF0C;&#x5373;&#x5B8C;&#x5168;&#x4E0D;&#x88C1;&#x526A;</li>
<li>&#x901A;&#x8FC7;setLevel&#x8BBE;&#x7F6E;&#x88C1;&#x526A;&#x5927;&#x5C0F;</li>
</ul>
</li>
<li><p>gravity&#xFF1A;&#x548C;View&#x7684;&#x5173;&#x7CFB;&#xFF0C;&#x4E5F;&#x548C;&#x88C1;&#x526A;&#x65B9;&#x5411;&#x4E00;&#x8D77;&#x5F71;&#x54CD;&#x88C1;&#x526A;&#x884C;&#x4E3A;</p>
<ul>
<li>top    &#x5C06;&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#x653E;&#x5728;&#x5BB9;&#x5668;&#x7684;&#x9876;&#x90E8;&#xFF0C;&#x4E0D;&#x6539;&#x53D8;&#x5176;&#x5927;&#x5C0F;&#x3002;&#x5F53;clipOrientation &#x662F;&quot;vertical&quot;&#xFF0C;&#x88C1;&#x526A;&#x53D1;&#x751F;&#x5728;drawable&#x7684;&#x5E95;&#x90E8;&#xFF08;bottom&#xFF09;</li>
<li>bottom    &#x5C06;&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#x653E;&#x5728;&#x5BB9;&#x5668;&#x7684;&#x5E95;&#x90E8;&#xFF0C;&#x4E0D;&#x6539;&#x53D8;&#x5176;&#x5927;&#x5C0F;&#x3002;&#x5F53;clipOrientation &#x662F; &quot;vertical&quot;&#xFF0C;&#x88C1;&#x526A;&#x53D1;&#x751F;&#x5728;drawable&#x7684;&#x9876;&#x90E8;&#xFF08;top&#xFF09;</li>
<li>left    &#x5C06;&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#x653E;&#x5728;&#x5BB9;&#x5668;&#x7684;&#x5DE6;&#x90E8;&#xFF0C;&#x4E0D;&#x6539;&#x53D8;&#x5176;&#x5927;&#x5C0F;&#x3002;&#x5F53;clipOrientation &#x662F; &quot;horizontal&quot;&#xFF0C;&#x88C1;&#x526A;&#x53D1;&#x751F;&#x5728;drawable&#x7684;&#x53F3;&#x8FB9;&#xFF08;right&#xFF09;&#x3002;&#x9ED8;&#x8BA4;&#x7684;&#x662F;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x3002;</li>
<li>right    &#x5C06;&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#x653E;&#x5728;&#x5BB9;&#x5668;&#x7684;&#x53F3;&#x90E8;&#xFF0C;&#x4E0D;&#x6539;&#x53D8;&#x5176;&#x5927;&#x5C0F;&#x3002;&#x5F53;clipOrientation &#x662F; &quot;horizontal&quot;&#xFF0C;&#x88C1;&#x526A;&#x53D1;&#x751F;&#x5728;drawable&#x7684;&#x5DE6;&#x8FB9;&#xFF08;left&#xFF09;&#x3002;</li>
<li>center_vertical    &#x5C06;&#x5BF9;&#x8C61;&#x653E;&#x5728;&#x5782;&#x76F4;&#x4E2D;&#x95F4;&#xFF0C;&#x4E0D;&#x6539;&#x53D8;&#x5176;&#x5927;&#x5C0F;&#x3002;&#x88C1;&#x526A;&#x7684;&#x60C5;&#x51B5;&#x548C;&#x201D;center&#x201C;&#x4E00;&#x6837;&#x3002;</li>
<li>fill_vertical    &#x5782;&#x76F4;&#x65B9;&#x5411;&#x4E0A;&#x4E0D;&#x53D1;&#x751F;&#x88C1;&#x526A;&#x3002;&#xFF08;&#x9664;&#x975E;drawable&#x7684;level&#x662F; 0&#xFF0C;&#x624D;&#x4F1A;&#x4E0D;&#x53EF;&#x89C1;&#xFF0C;&#x8868;&#x793A;&#x5168;&#x90E8;&#x88C1;&#x526A;&#x5B8C;&#xFF09;</li>
<li>center_horizontal    &#x5C06;&#x5BF9;&#x8C61;&#x653E;&#x5728;&#x6C34;&#x5E73;&#x4E2D;&#x95F4;&#xFF0C;&#x4E0D;&#x6539;&#x53D8;&#x5176;&#x5927;&#x5C0F;&#x3002;&#x88C1;&#x526A;&#x7684;&#x60C5;&#x51B5;&#x548C;&#x201D;center&#x201C;&#x4E00;&#x6837;&#x3002;</li>
<li>fill_horizontal    &#x6C34;&#x5E73;&#x65B9;&#x5411;&#x4E0A;&#x4E0D;&#x53D1;&#x751F;&#x88C1;&#x526A;&#x3002;&#xFF08;&#x9664;&#x975E;drawable&#x7684;level&#x662F; 0&#xFF0C;&#x624D;&#x4F1A;&#x4E0D;&#x53EF;&#x89C1;&#xFF0C;&#x8868;&#x793A;&#x5168;&#x90E8;&#x88C1;&#x526A;&#x5B8C;&#xFF09;</li>
<li>center    &#x5C06;&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#x653E;&#x5728;&#x6C34;&#x5E73;&#x5782;&#x76F4;&#x5750;&#x6807;&#x7684;&#x4E2D;&#x95F4;&#xFF0C;&#x4E0D;&#x6539;&#x53D8;&#x5176;&#x5927;&#x5C0F;&#x3002;&#x5F53;clipOrientation &#x662F; &quot;horizontal&quot;&#x88C1;&#x526A;&#x53D1;&#x751F;&#x5728;&#x5DE6;&#x53F3;&#x3002;&#x5F53;clipOrientation&#x662F;&quot;vertical&quot;,&#x88C1;&#x526A;&#x53D1;&#x751F;&#x5728;&#x4E0A;&#x4E0B;&#x3002;</li>
<li>fill    &#x586B;&#x5145;&#x6574;&#x4E2A;&#x5BB9;&#x5668;&#xFF0C;&#x4E0D;&#x4F1A;&#x53D1;&#x751F;&#x88C1;&#x526A;&#x3002;(&#x9664;&#x975E;drawable&#x7684;level&#x662F; 0&#xFF0C;&#x624D;&#x4F1A;&#x4E0D;&#x53EF;&#x89C1;&#xFF0C;&#x8868;&#x793A;&#x5168;&#x90E8;&#x88C1;&#x526A;&#x5B8C;)&#x3002;</li>
<li>clip_vertical   &#x989D;&#x5916;&#x7684;&#x9009;&#x9879;&#xFF0C;&#x5B83;&#x80FD;&#x591F;&#x628A;&#x5B83;&#x7684;&#x5BB9;&#x5668;&#x7684;&#x4E0A;&#x4E0B;&#x8FB9;&#x754C;&#xFF0C;&#x8BBE;&#x7F6E;&#x4E3A;&#x5B50;&#x5BF9;&#x8C61;&#x7684;&#x4E0A;&#x4E0B;&#x8FB9;&#x7F18;&#x7684;&#x88C1;&#x526A;&#x8FB9;&#x754C;&#x3002;&#x88C1;&#x526A;&#x8981;&#x57FA;&#x4E8E;&#x5BF9;&#x8C61;&#x5782;&#x76F4;&#x91CD;&#x529B;&#x8BBE;&#x7F6E;&#xFF1A;&#x5982;&#x679C;&#x91CD;&#x529B;&#x8BBE;&#x7F6E;&#x4E3A;top&#xFF0C;&#x5219;&#x88C1;&#x526A;&#x4E0B;&#x8FB9;&#xFF0C;&#x5982;&#x679C;&#x8BBE;&#x7F6E;&#x4E3A;bottom&#xFF0C;&#x5219;&#x88C1;&#x526A;&#x4E0A;&#x8FB9;&#xFF0C;&#x5426;&#x5219;&#x5219;&#x4E0A;&#x4E0B;&#x4E24;&#x8FB9;&#x90FD;&#x8981;&#x88C1;&#x526A;&#x3002;</li>
<li>clip_horizontal  &#x989D;&#x5916;&#x7684;&#x9009;&#x9879;&#xFF0C;&#x5B83;&#x80FD;&#x591F;&#x628A;&#x5B83;&#x7684;&#x5BB9;&#x5668;&#x7684;&#x5DE6;&#x53F3;&#x8FB9;&#x754C;&#xFF0C;&#x8BBE;&#x7F6E;&#x4E3A;&#x5B50;&#x5BF9;&#x8C61;&#x7684;&#x5DE6;&#x53F3;&#x8FB9;&#x7F18;&#x7684;&#x88C1;&#x526A;&#x8FB9;&#x754C;&#x3002;&#x88C1;&#x526A;&#x8981;&#x57FA;&#x4E8E;&#x5BF9;&#x8C61;&#x5782;&#x76F4;&#x91CD;&#x529B;&#x8BBE;&#x7F6E;</li>
</ul>
</li>
<li>clipOrientation&#xFF1A;&#x88C1;&#x526A;&#x65B9;&#x5411;<ul>
<li>horizontal    &#x6C34;&#x5E73;&#x65B9;&#x5411;&#x88C1;&#x526A;</li>
<li>vertical    &#x5782;&#x76F4;&#x65B9;&#x5411;&#x88C1;&#x526A;</li>
</ul>
</li>
</ul>
</li>
</ul>
<p><strong>java&#x4EE3;&#x7801;</strong></p>
<pre><code class="lang-java">ClipDrawable clipDrawable = <span class="hljs-keyword">new</span> ClipDrawable(
        getResources().getDrawable(R.drawable.animate_shower),
        Gravity.CENTER,
        ClipDrawable.VERTICAL);

<span class="hljs-keyword">final</span> View v = findViewById(R.id.tv);
v.setBackground(clipDrawable);
v.getBackground().setLevel(<span class="hljs-number">100</span>);
</code></pre>
<h3 id="insetdrawable">InsetDrawable</h3>
<ul>
<li>InsetDrawable&#x672C;&#x8EAB;&#x662F;&#x4E00;&#x5F20;Drawable&#xFF0C;&#x518D;&#x5F80;&#x91CC;&#x5D4C;&#x5165;&#x4E00;&#x4E2A;&#xFF1A;<ul>
<li>&#x5F53;&#x63A7;&#x4EF6;&#x9700;&#x8981;&#x7684;&#x80CC;&#x666F;&#x6BD4;&#x5B9E;&#x9645;&#x7684;&#x8FB9;&#x6846;&#x5C0F;&#x7684;&#x65F6;&#x5019;&#x6BD4;&#x8F83;&#x9002;&#x5408;&#x4F7F;&#x7528;InsetDrawable&#xFF0C;&#x6307;&#x5B9A;&#x4E86;&#x4E0A;&#x4E0B;&#x5DE6;&#x53F3;&#x7A7A;&#x51FA;&#x7684;&#x8DDD;&#x79BB;</li>
<li>&#x6CE8;&#x610F;&#xFF1A;&#x5BF9;&#x5E94;View&#x7684;&#x5185;&#x5BB9;&#x4E5F;&#x4EE5;InsetDRAWable&#x7684;&#x8FB9;&#x8DDD;&#x4E3A;&#x8FB9;&#x8DDD;</li>
</ul>
</li>
</ul>
<p><strong>xml</strong></p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">inset</span> <span class="hljs-attr">xmlns:android</span>=<span class="hljs-string">&quot;http://schemas.android.com/apk/res/android&quot;</span>   
                <span class="hljs-attr">android:drawable</span>=<span class="hljs-string">&quot;@drawable/image4&quot;</span>  
                <span class="hljs-attr">android:insetLeft</span>=<span class="hljs-string">&quot;50dp&quot;</span>  
                <span class="hljs-attr">android:insetRight</span>=<span class="hljs-string">&quot;50dp&quot;</span>  
                <span class="hljs-attr">android:insetTop</span>=<span class="hljs-string">&quot;20dp&quot;</span>  
                <span class="hljs-attr">android:insetBottom</span>=<span class="hljs-string">&quot;20dp&quot;</span>&gt;</span>  
            <span class="hljs-tag">&lt;/<span class="hljs-name">inset</span>&gt;</span>
</code></pre>
<p><strong>java</strong></p>
<pre><code class="lang-java">InsetDrawable insetDrawable=<span class="hljs-keyword">new</span> InsetDrawable(getResources().getDrawable(R.drawable.animate_shower), <span class="hljs-number">20</span>, <span class="hljs-number">30</span>, <span class="hljs-number">30</span>, <span class="hljs-number">20</span>);
</code></pre>
<h3 id="layerdrawable">LayerDrawable</h3>
<ul>
<li>&#x521B;&#x5EFA;LayerDrawable&#x548C;&#x4F7F;&#x7528;<ul>
<li>&#x4E00;&#x4E2A;LayerDrawable&#x662F;&#x4E00;&#x4E2A;&#x53EF;&#x4EE5;&#x7BA1;&#x7406;&#x4E00;&#x7EC4;drawable&#x5BF9;&#x8C61;&#x7684;drawable&#x3002;</li>
<li>&#x5728;LayerDrawable&#x7684;drawable&#x8D44;&#x6E90;&#x6309;&#x7167;&#x5217;&#x8868;&#x7684;&#x987A;&#x5E8F;&#x7ED8;&#x5236;&#xFF0C;&#x5217;&#x8868;&#x7684;&#x6700;&#x540E;&#x4E00;&#x4E2A;drawable&#x7ED8;&#x5236;&#x5728;&#x6700;&#x4E0A;&#x5C42;&#x3002;</li>
<li>&#x5B83;&#x6240;&#x5305;&#x542B;&#x7684;&#x4E00;&#x7EC4;drawable&#x8D44;&#x6E90;&#x7528;&#x591A;&#x4E2A;<item>&#x5143;&#x7D20;&#x8868;&#x793A;&#xFF0C;&#x4E00;&#x4E2A;<item>&#x5143;&#x7D20;&#x4EE3;&#x8868;&#x4E00;&#x4E2A;drawable&#x8D44;&#x6E90;&#x3002;</item></item></li>
</ul>
</li>
</ul>
<p>&#x5728;&#x9ED8;&#x8BA4;&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x6240;&#x6709;&#x7684;drawable item&#x90FD;&#x4F1A;&#x7F29;&#x653E;&#x5230;&#x5408;&#x9002;&#x7684;&#x5927;&#x5C0F;&#x6765;&#x9002;&#x5E94;&#x89C6;&#x56FE;&#x3002;<br>&#x56E0;&#x6B64;&#xFF0C;&#x5728;&#x4E00;&#x4E2A;layer-list&#x4E2D;&#x5B9A;&#x4E49;&#x4E0D;&#x540C;&#x7684;&#x4F4D;&#x7F6E;&#x53EF;&#x80FD;&#x4F1A;&#x589E;&#x52A0;&#x89C6;&#x56FE;&#x7684;&#x5C3A;&#x5BF8;&#x548C;&#x88AB;&#x81EA;&#x52A8;&#x7F29;&#x653E;&#x3002;  &#x4E3A;&#x4E86;&#x907F;&#x514D;&#x88AB;&#x7F29;&#x653E;&#xFF0C;&#x53EF;&#x4EE5;&#x5728;<item>&#x8282;&#x70B9;&#x91CC;&#x52A0;&#x4E0A;<bitmap>&#x5143;&#x7D20;&#x6765;&#x6307;&#x5B9A;&#x4E00;&#x4E2A;drawable&#xFF0C;  &#x5E76;&#x4E14;&#x5B9A;&#x4E49;&#x4E00;&#x4E9B;&#x4E0D;&#x4F1A;&#x88AB;&#x62C9;&#x4F38;&#x7684;gravity&#x5C5E;&#x6027;&#xFF0C;&#x4F8B;&#x5982;center&#x3002;<br>&#x4E3E;&#x4E2A;&#x4F8B;&#x5B50;&#xFF0C;&#x4E0B;&#x9762;&#x5728;item&#x91CC;&#x9762;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;drawable&#xFF0C;&#x56FE;&#x7247;&#x5C31;&#x4F1A;&#x81EA;&#x52A8;&#x7F29;&#x653E;&#x4EE5;&#x9002;&#x5E94;&#x89C6;&#x56FE;&#x7684;&#x5927;&#x5C0F;&#x3002;</bitmap></item></p>
<p><strong>&#x53EF;&#x80FD;&#x4F1A;&#x53D1;&#x751F;&#x62C9;&#x4F38;&#x7684;&#x7528;&#x6CD5;</strong></p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">item</span> <span class="hljs-attr">android:drawable</span>=<span class="hljs-string">&quot;@drawable/image&quot;</span> /&gt;</span>
</code></pre>
<p><strong>&#x4E3A;&#x4E86;&#x907F;&#x514D;&#x7F29;&#x653E;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<bitmap>&#x7684;&#x5B50;&#x5143;&#x7D20;&#x6765;&#x6307;&#x5B9A;drawable&#x8D44;&#x6E90;</bitmap></strong></p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">item</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">bitmap</span> <span class="hljs-attr">android:src</span>=<span class="hljs-string">&quot;@drawable/image&quot;</span>
          <span class="hljs-attr">android:gravity</span>=<span class="hljs-string">&quot;center&quot;</span> /&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">item</span>&gt;</span>
</code></pre>
<p><strong>&#x4F8B;&#x5B50;&#xFF1A;&#x5728;xml&#x6587;&#x4EF6;&#x4E2D;&#x5B9A;&#x4E49;layerDrawable&#xFF0C;&#x7136;&#x540E;&#x4F7F;&#x7528;</strong></p>
<pre><code class="lang-xml"><span class="php"><span class="hljs-meta">&lt;?</span>xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;utf-8&quot;</span><span class="hljs-meta">?&gt;</span></span>
<span class="hljs-tag">&lt;<span class="hljs-name">layer-list</span> <span class="hljs-attr">xmlns:android</span>=<span class="hljs-string">&quot;http://schemas.android.com/apk/res/android&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">item</span> <span class="hljs-attr">android:drawable</span>=<span class="hljs-string">&quot;@drawable/black_lotus&quot;</span>
        <span class="hljs-attr">android:left</span>=<span class="hljs-string">&quot;20dp&quot;</span>
        <span class="hljs-attr">android:top</span>=<span class="hljs-string">&quot;20dp&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">item</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">item</span> <span class="hljs-attr">android:drawable</span>=<span class="hljs-string">&quot;@drawable/black_lotus&quot;</span>
        <span class="hljs-attr">android:left</span>=<span class="hljs-string">&quot;40dp&quot;</span>
        <span class="hljs-attr">android:top</span>=<span class="hljs-string">&quot;40dp&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">item</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">item</span> <span class="hljs-attr">android:drawable</span>=<span class="hljs-string">&quot;@drawable/black_lotus&quot;</span>
        <span class="hljs-attr">android:left</span>=<span class="hljs-string">&quot;60dp&quot;</span>
        <span class="hljs-attr">android:top</span>=<span class="hljs-string">&quot;60dp&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">item</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">layer-list</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">ImageView</span>
    <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;wrap_content&quot;</span>
    <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;wrap_content&quot;</span>
    <span class="hljs-attr">android:src</span>=<span class="hljs-string">&quot;@drawable/layers&quot;</span> /&gt;</span>
</code></pre>
<p><strong>&#x4EE3;&#x7801;&#x63A7;&#x5236;</strong></p>
<pre><code class="lang-java">Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.animate_shower);  
Drawable[] drawables=<span class="hljs-keyword">new</span> Drawable[<span class="hljs-number">3</span>];  
<span class="hljs-comment">//drawables[0]=new PaintDrawable(Color.BLACK);  </span>
<span class="hljs-comment">//drawables[1]=new PaintDrawable(Color.BLUE);  </span>
drawables[<span class="hljs-number">0</span>]=<span class="hljs-keyword">new</span> BitmapDrawable(bitmap);  
drawables[<span class="hljs-number">1</span>]=<span class="hljs-keyword">new</span> BitmapDrawable(bitmap);  
drawables[<span class="hljs-number">2</span>]=<span class="hljs-keyword">new</span> BitmapDrawable(bitmap);  
LayerDrawable layer=<span class="hljs-keyword">new</span> LayerDrawable(drawables);  
layer.setLayerInset(<span class="hljs-number">0</span>, <span class="hljs-number">20</span>, <span class="hljs-number">20</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>); <span class="hljs-comment">//index&#xFF08;&#x7B2C;&#x51E0;&#x4E2A;Drawable&#xFF09;, &#x5DE6;&#xFF0C;&#x4E0A;&#xFF0C;&#x53F3;&#xFF0C;&#x4E0B;--&#x50CF;&#x7D20;&#xFF0C;&#x8BF4;&#x7684;&#x662F;&#x5DE6;&#x4E0A;&#x89D2;&#x76F8;&#x5BF9;&#x4E8E;LayerDrawable&#x5DE6;&#x4E0A;&#x89D2;&#x7684;&#x4F4D;&#x7F6E; &#xFF0C;&#x548C;&#x53F3;&#x4E0B;&#x89D2;&#xFF08;&#x540E;&#x4E24;&#x4E2A;&#x53C2;&#x6570;&#xFF09;&#x76F8;&#x5BF9;&#x4E8E;LayerDrawable&#x53F3;&#x4E0B;&#x89D2;&#x7684;&#x4F4D;&#x7F6E;</span>
layer.setLayerInset(<span class="hljs-number">1</span>, <span class="hljs-number">40</span>, <span class="hljs-number">40</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>);  
layer.setLayerInset(<span class="hljs-number">2</span>, <span class="hljs-number">60</span>, <span class="hljs-number">60</span>, <span class="hljs-number">60</span>, <span class="hljs-number">60</span>);  

 View v = findViewById(R.id.tv);
 <span class="hljs-comment">//v.setBackground(layer);//--&#x7528;&#x4EE3;&#x7801;&#x4E2D;&#x521B;&#x5EFA;&#x7684;LayerDrawable&#xFF0C;&#x5728;&#x8FD9;&#x91CC;&#x56FE;&#x7247;&#x4F1A;&#x88AB;&#x62C9;&#x4F38;</span>
 v.setBackgroundResource(R.drawable.d_layer_drawable);
</code></pre>
<p><strong>&#x4E0A;&#x4F8B;&#x4E2D;&#x6CA1;&#x6709;&#x88AB;&#x62C9;&#x4F38;&#x7684;xml&#x5B9A;&#x4E49;</strong></p>
<pre><code class="lang-xml"><span class="php"><span class="hljs-meta">&lt;?</span>xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;utf-8&quot;</span><span class="hljs-meta">?&gt;</span></span>
<span class="hljs-tag">&lt;<span class="hljs-name">layer-list</span> <span class="hljs-attr">xmlns:android</span>=<span class="hljs-string">&quot;http://schemas.android.com/apk/res/android&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">item</span> <span class="hljs-attr">android:drawable</span>=<span class="hljs-string">&quot;@drawable/animate_shower&quot;</span>
        <span class="hljs-attr">android:left</span>=<span class="hljs-string">&quot;20dp&quot;</span>
        <span class="hljs-attr">android:top</span>=<span class="hljs-string">&quot;20dp&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">item</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">item</span> <span class="hljs-attr">android:drawable</span>=<span class="hljs-string">&quot;@drawable/animate_shower&quot;</span>
        <span class="hljs-attr">android:left</span>=<span class="hljs-string">&quot;40dp&quot;</span>
        <span class="hljs-attr">android:top</span>=<span class="hljs-string">&quot;40dp&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">item</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">item</span>
        <span class="hljs-attr">android:left</span>=<span class="hljs-string">&quot;60dp&quot;</span>
        <span class="hljs-attr">android:top</span>=<span class="hljs-string">&quot;60dp&quot;</span>&gt;</span>&gt;
      <span class="hljs-tag">&lt;<span class="hljs-name">bitmap</span> <span class="hljs-attr">android:src</span>=<span class="hljs-string">&quot;@drawable/animate_shower&quot;</span>
              <span class="hljs-attr">android:gravity</span>=<span class="hljs-string">&quot;top&quot;</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">item</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">layer-list</span>&gt;</span>
</code></pre>
<h3 id="leveldrawable">LevelDrawable</h3>
<ul>
<li><p>&#x7B80;&#x4ECB;&#xFF1A;</p>
<ul>
<li>&#x5916;&#x754C;&#x5B58;&#x5728;&#x4E00;&#x4E2A;&#x6570;&#x503C;&#x8303;&#x56F4;&#xFF0C;&#x63A7;&#x4EF6;&#x8981;&#x6839;&#x636E;&#x8FD9;&#x4E2A;&#x6570;&#x503C;&#x5207;&#x6362;&#x80CC;&#x666F;&#x6216;&#x8005;&#x56FE;&#x6807;&#xFF0C;</li>
<li>&#x5C31;&#x53EF;&#x4EE5;&#x7528;LevelDrawable</li>
<li><p>&#x7535;&#x6E90;&#x663E;&#x793A;&#x5C31;&#x662F;&#x7528;&#x7684;LevelDrawable</p>
<p>&#x8C03;&#x7528;Drawable&#x7684;setLevel()&#x65B9;&#x6CD5;&#x53EF;&#x4EE5;&#x52A0;&#x8F7D;level-list&#x6216;&#x4EE3;&#x7801;&#x4E2D;&#x5B9A;&#x4E49;&#x7684;&#x67D0;&#x4E2A;drawable&#x8D44;&#x6E90;&#xFF0C;<br>&#x5224;&#x65AD;&#x52A0;&#x8F7D;&#x67D0;&#x9879;&#x7684;&#x65B9;&#x5F0F;&#xFF1A;level-list&#x4E2D;&#x67D0;&#x9879;&#x7684;android:maxLevel&#x6570;&#x503C;&#x5927;&#x4E8E;&#x6216;&#x8005;&#x7B49;&#x4E8E;setLevel&#x8BBE;&#x7F6E;&#x7684;&#x6570;&#x503C;&#xFF0C;&#x5C31;&#x4F1A;&#x88AB;&#x52A0;&#x8F7D;&#x3002;</p>
</li>
</ul>
</li>
<li><p>&#x4F7F;&#x7528;LevelDrawable&#x6CE8;&#x610F;&#x51E0;&#x70B9;&#xFF1A;</p>
<ul>
<li>&#x9ED8;&#x8BA4;&#x7684;level&#x4E3A;0&#xFF0C;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x548C;0&#x5339;&#x914D;&#x7684;level&#xFF0C;&#x90A3;&#x4E48;&#x5C31;&#x4E0D;&#x663E;&#x793A;&#x3002;</li>
<li>level&#x5339;&#x914D;&#x4EE5;maxLevel&#x4F18;&#x5148;&#x3002;&#x5373;&#x5982;&#x679C;&#x6709;&#x4E2A;item&#xFF0C;min&#xFF1A;1&#xFF0C;max&#xFF1A;2&#x3002;   &#x53E6;&#x4E00;&#x4EFD;item&#xFF0C;min&#xFF1A;2&#xFF0C;max&#xFF1A;3&#x3002;</li>
<li>&#x5982;&#x679C;&#x6B64;&#x65F6;&#x8BBE;&#x7F6E;level=2&#xFF0C;&#x90A3;&#x4E48;&#x4F1A;&#x5339;&#x914D;&#x7B2C;&#x4E00;&#x4E2A;item&#x3002;</li>
</ul>
</li>
</ul>
<p><strong>&#x4EE3;&#x7801;&#x63A7;&#x5236;</strong></p>
<pre><code class="lang-java">LevelListDrawable levelListDrawable = <span class="hljs-keyword">new</span> LevelListDrawable();
levelListDrawable.addLevel(<span class="hljs-number">0</span>, <span class="hljs-number">20</span>, getResources().getDrawable(R.drawable.animate_shower)); <span class="hljs-comment">//min, max, drawable</span>
levelListDrawable.addLevel(<span class="hljs-number">20</span>, <span class="hljs-number">40</span>, getResources().getDrawable(R.drawable.kaola));
levelListDrawable.addLevel(<span class="hljs-number">40</span>, <span class="hljs-number">70</span>, getResources().getDrawable(R.drawable.qie));
levelListDrawable.addLevel(<span class="hljs-number">70</span>, <span class="hljs-number">100</span>, getResources().getDrawable(R.drawable.shuimu));

<span class="hljs-keyword">final</span> View v = findViewById(R.id.tv);
v.setBackground(levelListDrawable);

<span class="hljs-comment">//&#x6539;&#x53D8;&#x80CC;&#x666F;&#x7684;level&#xFF1A;</span>
v.getBackground().setLevel(progress);
</code></pre>
<h3 id="statelistdrawable">StateListDrawable</h3>
<p><strong>&#x4EE3;&#x7801;&#x63A7;&#x5236;</strong></p>
<pre><code class="lang-java">StateListDrawable drawable=<span class="hljs-keyword">new</span> StateListDrawable();
<span class="hljs-comment">//&#x5982;&#x679C;&#x8981;&#x8BBE;&#x7F6E;&#x83AB;&#x9879;&#x4E3A;false&#xFF0C;&#x5728;&#x524D;&#x9762;&#x52A0;&#x8D1F;&#x53F7; &#xFF0C;&#x6BD4;&#x5982;android.R.attr.state_focesed&#x6807;&#x5FD7;true&#xFF0C;-android.R.attr.state_focesed&#x5C31;&#x6807;&#x5FD7;false</span>
drawable.addState(<span class="hljs-keyword">new</span> <span class="hljs-keyword">int</span>[]{android.R.attr.state_focused}, <span class="hljs-keyword">this</span>.getResources().getDrawable(R.drawable.kaola));
drawable.addState(<span class="hljs-keyword">new</span> <span class="hljs-keyword">int</span>[]{android.R.attr.state_pressed}, <span class="hljs-keyword">this</span>.getResources().getDrawable(R.drawable.qie));
drawable.addState(<span class="hljs-keyword">new</span> <span class="hljs-keyword">int</span>[]{android.R.attr.state_selected}, <span class="hljs-keyword">this</span>.getResources().getDrawable(R.drawable.shuimu));
drawable.addState(<span class="hljs-keyword">new</span> <span class="hljs-keyword">int</span>[]{}, <span class="hljs-keyword">this</span>.getResources().getDrawable(R.drawable.shuimu));<span class="hljs-comment">//&#x9ED8;&#x8BA4;</span>

View v = findViewById(R.id.tv);
v.setBackground(drawable);
</code></pre>
<p><strong>xml&#x63A7;&#x5236;</strong></p>
<pre><code class="lang-xml"><span class="php"><span class="hljs-meta">&lt;?</span>xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;UTF-8&quot;</span><span class="hljs-meta">?&gt;</span></span>  
            <span class="hljs-tag">&lt;<span class="hljs-name">selector</span>  
              <span class="hljs-attr">xmlns:android</span>=<span class="hljs-string">&quot;http://schemas.android.com/apk/res/android&quot;</span>&gt;</span>  
                <span class="hljs-tag">&lt;<span class="hljs-name">item</span> <span class="hljs-attr">android:state_focused</span>=<span class="hljs-string">&quot;true&quot;</span> <span class="hljs-attr">android:drawable</span>=<span class="hljs-string">&quot;@drawable/botton_add&quot;</span> /&gt;</span>  
                <span class="hljs-tag">&lt;<span class="hljs-name">item</span> <span class="hljs-attr">android:state_pressed</span>=<span class="hljs-string">&quot;true&quot;</span> <span class="hljs-attr">android:drawable</span>=<span class="hljs-string">&quot;@drawable/botton_add_down&quot;</span> /&gt;</span>  
                <span class="hljs-tag">&lt;<span class="hljs-name">item</span> <span class="hljs-attr">android:state_selected</span>=<span class="hljs-string">&quot;true&quot;</span> <span class="hljs-attr">android:drawable</span>=<span class="hljs-string">&quot;@drawable/botton_add&quot;</span> /&gt;</span>  
                <span class="hljs-tag">&lt;<span class="hljs-name">item</span> <span class="hljs-attr">android:drawable</span>=<span class="hljs-string">&quot;@drawable/botton_add&quot;</span> /&gt;</span>//&#x9ED8;&#x8BA4;  
            <span class="hljs-tag">&lt;/<span class="hljs-name">selector</span>&gt;</span>
</code></pre>
<ul>
<li>&#x6CE8;&#x610F;&#xFF1A;<ul>
<li>android:drawable&#x5FC5;&#x987B;&#x7684;&#xFF0C;&#x6307;&#x5411;&#x4E00;&#x4E2A;drawable&#x8D44;&#x6E90;</li>
<li>&#x987A;&#x5E8F;&#x5F88;&#x91CD;&#x8981;&#xFF1A;    <ul>
<li>android:state_pressed  &#x662F;&#x5426;&#x6309;&#x4E0B;</li>
<li>android:state_focused  &#x662F;&#x5426;&#x83B7;&#x5F97;&#x83B7;&#x5F97;&#x7126;&#x70B9;</li>
<li>android:state_hovered  &#x9F20;&#x6807;&#x5728;&#x4E0A;&#x9762;&#x6ED1;&#x52A8;&#x7684;&#x72B6;&#x6001;&#x3002;&#x901A;&#x5E38;&#x548C;state_focused&#x4F7F;&#x7528;&#x540C;&#x6837;&#x7684;drawable</li>
<li>android:state_selected &#x662F;&#x5426;&#x9009;&#x4E2D;</li>
<li>android:state_checkable &#x662F;&#x5426;&#x53EF;&#x4EE5;&#x88AB;&#x52FE;&#x9009;&#xFF08;checkable&#xFF09;&#x3002;&#x53EA;&#x80FD;&#x7528;&#x5728;&#x53EF;&#x4EE5;&#x52FE;&#x9009;&#x7684;&#x63A7;&#x4EF6;</li>
<li>android:state_checked &#x662F;&#x5426;&#x88AB;&#x52FE;&#x9009;&#x4E0A;</li>
<li>android:state_enabled &#x662F;&#x5426;&#x53EF;&#x7528;</li>
<li>android:state_activated &#x662F;&#x5426;&#x88AB;&#x6FC0;&#x6D3B;&#x5E76;&#x6301;&#x4E45;&#x7684;&#x9009;&#x62E9;</li>
<li>android:state_window_focused &#x5F53;&#x524D;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x662F;&#x5426;&#x83B7;&#x5F97;&#x7126;&#x70B9;</li>
</ul>
</li>
<li>&#x6CE8;&#x610F;&#xFF1A;Android&#x7CFB;&#x7EDF;&#x5C06;&#x4F1A;&#x9009;&#x4E2D;&#x7B2C;&#x4E00;&#x6761;&#x7B26;&#x5408;&#x5F53;&#x524D;&#x72B6;&#x6001;&#x7684;item&#x3002;</li>
<li>&#x56E0;&#x6B64;&#xFF0C;&#x5982;&#x679C;&#x7B2C;&#x4E00;&#x9879;&#x5217;&#x8868;&#x4E2D;&#x5305;&#x542B;&#x4E86;&#x6240;&#x6709;&#x7684;&#x72B6;&#x6001;&#x5C5E;&#x6027;&#xFF0C;&#x90A3;&#x4E48;&#x5B83;&#x662F;&#x6BCF;&#x6B21;&#x5C31;&#x53EA;&#x7528;&#x4ED6;</li>
<li>&#x8FD9;&#x5C31;&#x662F;&#x4E3A;&#x4EC0;&#x4E48;&#x4F60;&#x7684;&#x9ED8;&#x8BA4;&#x503C;&#x5E94;&#x8BE5;&#x653E;&#x5728;&#x6700;&#x540E;&#x9762;&#x3002;</li>
<li>&#x4E5F;&#x5C31;&#x8BF4;&#x8FD9;&#x4E9B;&#x72B6;&#x6001;&#x6240;&#x5360;&#x8303;&#x56F4;&#x8D8A;&#x5927;&#x7684;&#xFF0C;&#x8D8A;&#x5FC5;&#x987B;&#x653E;&#x540E;&#x9762;&#xFF0C;&#x57FA;&#x672C;&#x4E0A;&#x5C31;&#x7167;&#x7740;&#x4E0A;&#x9762;&#x7684;&#x987A;&#x5E8F;&#x653E;&#x5C31;&#x884C;</li>
</ul>
</li>
</ul>
<h3 id="transitiondrawable">TransitionDrawable</h3>
<p>&#x5B9E;&#x73B0;&#x4E24;&#x5F20;Drawable&#x4E4B;&#x95F4;&#x7684;&#x6E10;&#x5165;&#x6E10;&#x51FA;&#x5207;&#x6362;&#xFF1A;&#x5C31;&#x4E24;&#x5F20;&#xFF0C;&#x53EA;&#x80FD;&#x6E10;&#x9690;&#x6E10;&#x73B0;&#xFF0C;&#x5C31;&#x4E00;&#x6B21;&#xFF0C;&#x6CA1;&#x6CD5;&#x5FAA;&#x73AF;&#x7684;&#x6765;</p>
<p><strong>xml</strong></p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">transition</span> <span class="hljs-attr">xmlns:android</span>=<span class="hljs-string">&quot;http://schemas.android.com/apk/res/android&quot;</span> &gt;</span>  
                <span class="hljs-tag">&lt;<span class="hljs-name">item</span> <span class="hljs-attr">android:drawable</span>=<span class="hljs-string">&quot;@drawable/image01&quot;</span>/&gt;</span>  
                <span class="hljs-tag">&lt;<span class="hljs-name">item</span> <span class="hljs-attr">android:drawable</span>=<span class="hljs-string">&quot;@drawable/image02&quot;</span>/&gt;</span>  
            <span class="hljs-tag">&lt;/<span class="hljs-name">transition</span>&gt;</span>
</code></pre>
<p><strong>&#x4EE3;&#x7801;&#x63A7;&#x5236;</strong></p>
<pre><code class="lang-java">TransitionDrawable transitionDrawable=<span class="hljs-keyword">null</span>;  
transitionDrawable= <span class="hljs-keyword">new</span> TransitionDrawable(<span class="hljs-keyword">new</span> Drawable[] {
    getResources().getDrawable(R.drawable.animate_shower),
    getResources().getDrawable(R.drawable.kaola),
});
findViewById(R.id.tv).setBackground(transitionDrawable);
transitionDrawable.startTransition(<span class="hljs-number">3000</span>);<span class="hljs-comment">//&#x95F4;&#x9694;3&#x79D2;</span>
</code></pre>
<h3 id="rotatedrawable">RotateDrawable</h3>
<p>&#x65CB;&#x8F6C;&#xFF0C;&#x8303;&#x56F4;1&#x5230;10000&#xFF0C; &#x5BF9;&#x5E94;&#x89D2;&#x5EA6;&#x662F;xml&#x6307;&#x5B9A;&#x7684;<br>android:fromDegrees=&quot;-90&quot; &#x548C;android:toDegrees=&quot;180&quot;<br>&#x9009;&#x4E2D;&#x4E2D;&#x5FC3;&#x662F;xml&#x91CC;&#x6307;&#x5B9A;&#x7684;</p>
<p><strong>xml&#x63A7;&#x5236;</strong></p>
<pre><code class="lang-xml"><span class="php"><span class="hljs-meta">&lt;?</span>xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;utf-8&quot;</span><span class="hljs-meta">?&gt;</span></span>  
            <span class="hljs-tag">&lt;<span class="hljs-name">rotate</span> <span class="hljs-attr">xmlns:android</span>=<span class="hljs-string">&quot;http://schemas.android.com/apk/res/android&quot;</span>  
                <span class="hljs-attr">android:drawable</span>=<span class="hljs-string">&quot;@drawable/image02&quot;</span>  
                <span class="hljs-attr">android:visible</span>=<span class="hljs-string">&quot;true&quot;</span>  
                <span class="hljs-attr">android:fromDegrees</span>=<span class="hljs-string">&quot;-90&quot;</span>   <span class="hljs-attr">---</span>&#x9006;&#x65F6;&#x9488;<span class="hljs-attr">90</span>&#x5EA6;
                <span class="hljs-attr">android:toDegrees</span>=<span class="hljs-string">&quot;180&quot;</span>    <span class="hljs-attr">---</span>&#x987A;&#x65F6;&#x9488;<span class="hljs-attr">180</span>&#x5EA6;&#xFF0C;&#x8303;&#x56F4;&#x662F;<span class="hljs-attr">270</span>&#x5EA6;&#xFF0C;&#x5BF9;&#x5E94;<span class="hljs-attr">1</span>&#x5230;<span class="hljs-attr">10000</span>
                <span class="hljs-attr">android:pivotX</span>=<span class="hljs-string">&quot;50%&quot;</span>  <span class="hljs-attr">---</span>&#x76F8;&#x5BF9;&#x4E8E;<span class="hljs-attr">Drawable</span>&#x81EA;&#x5DF1;
                <span class="hljs-attr">android:pivotY</span>=<span class="hljs-string">&quot;50%&quot;</span>&gt;</span>  ---&#x76F8;&#x5BF9;&#x4E8E;Drawable&#x81EA;&#x5DF1;
            <span class="hljs-tag">&lt;/<span class="hljs-name">rotate</span>&gt;</span>
</code></pre>
<pre><code class="lang-java">v.setBackgroundResource(R.drawable.d_rotate_drawable);

<span class="hljs-comment">//&#x63A7;&#x5236;&#x65CB;&#x8F6C;&#xFF0C;&#x6CE8;&#x610F;&#x662F;setLevel&#xFF0C;&#x8303;&#x56F4;&#x662F;1&#x5230;1W</span>
v.getBackground().setLevel(progress);
</code></pre>
<h3 id="scaledrawable">ScaleDrawable</h3>
<p>&#x7F29;&#x653E;&#xFF1A; setLevel()&#x8BBE;&#x7F6E;&#x7F29;&#x653E;&#x6BD4;&#x4F8B;&#xFF0C;&#x6700;&#x5927;&#x503C;10000</p>
<pre><code class="lang-java">ScaleDrawable scaleDrawable = <span class="hljs-keyword">new</span> ScaleDrawable(
        getResources().getDrawable(R.drawable.animate_shower), 
        Gravity.CENTER, 
        <span class="hljs-number">0.1f</span>,  <span class="hljs-comment">//&#x597D;&#x50CF;&#x662F;&#x7F29;&#x653E;&#x7684;&#x8303;&#x56F4;&#xFF0C;&#x7F29;&#x653E;&#x8303;&#x56F4;&#x662F;10%&#xFF0C;&#x5373;&#x589E;&#x52A0;10%&#x5C31;&#x7ED9;&#x4F60;&#x5230;100%&#xFF0C;&#x5373;&#x4ECE;90%&#x5F00;&#x59CB;&#x7F29;&#x653E;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;setLevel(1)&#x5C31;&#x662F;90%&#xFF0C;1&#x5230;10000&#x5BF9;&#x5E94;80%&#x5230;100%</span>
        <span class="hljs-number">0.0f</span>);<span class="hljs-comment">//&#x8FD9;&#x91CC;&#x7684;0&#x8868;&#x793A;&#x7F29;&#x653E;&#x8303;&#x56F4;&#x662F;0&#xFF0C;&#x5373;&#x7EB5;&#x5411;&#x4E0D;&#x7F29;&#x653E;</span>

<span class="hljs-keyword">final</span> View v = findViewById(R.id.tv);
v.setBackground(scaleDrawable);
v.getBackground().setLevel(<span class="hljs-number">0</span>);
</code></pre>
<pre><code class="lang-xml"><span class="php"><span class="hljs-meta">&lt;?</span>xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;utf-8&quot;</span><span class="hljs-meta">?&gt;</span></span>  
            <span class="hljs-tag">&lt;<span class="hljs-name">scale</span> <span class="hljs-attr">xmlns:android</span>=<span class="hljs-string">&quot;http://schemas.android.com/apk/res/android&quot;</span>  
                <span class="hljs-attr">android:scaleWidth</span>=<span class="hljs-string">&quot;50%&quot;</span>  
                <span class="hljs-attr">android:scaleHeight</span>=<span class="hljs-string">&quot;50%&quot;</span>  
                <span class="hljs-attr">android:drawable</span>=<span class="hljs-string">&quot;@drawable/image1&quot;</span>   
                <span class="hljs-attr">android:scaleGravity</span>=<span class="hljs-string">&quot;center_vertical|center_horizontal&quot;</span>  
                &gt;</span>  
            <span class="hljs-tag">&lt;/<span class="hljs-name">scale</span>&gt;</span>
</code></pre>
<h3 id="gradientdrawable&#x548C;shapedrawable">GradientDrawable&#x548C;ShapeDrawable</h3>
<p><strong>&#x4E0B;&#x9762;&#x662F;&#x5F15;&#x7528;&#x522B;&#x4EBA;&#x7684;&#x8BDD;&#xFF0C;&#x51FA;&#x5904;&#x5DF2;&#x7ECF;&#x627E;&#x4E0D;&#x5230;&#x4E86;&#xFF1A;</strong></p>
<ul>
<li>&#x5728;&#x7814;&#x7A76;Android Drawable&#x8D44;&#x6E90;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x53D1;&#x73B0;&#x4E86;&#x4E00;&#x4E2A;&#x5947;&#x602A;&#x7684;&#x95EE;&#x9898;&#x3002;&#x5728;&#x5B98;&#x65B9;API&#x4ECB;&#x7ECD;&#x4E2D;&#xFF1A;<ul>
<li>ShapeDrawable&#xFF1A;This object can be defined in an XML file with the <shape> element&#xFF08;&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#x53EF;&#x4EE5;&#x7528;<shape>&#x5143;&#x7D20;&#x5728;xml&#x6587;&#x4EF6;&#x4E2D;&#x5B9A;&#x4E49;&#xFF09;</shape></shape></li>
<li>GradientDrawable &#x4ECB;&#x7ECD;&#xFF1A;This object can be defined in an XML file with the <shape> element&#xFF08;&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#x53EF;&#x4EE5;&#x7528;<shape>&#x5143;&#x7D20;&#x5728;xml&#x6587;&#x4EF6;&#x4E2D;&#x5B9A;&#x4E49;&#xFF09;</shape></shape></li>
<li>&#x4E24;&#x8005;&#x7684;&#x4ECB;&#x7ECD;&#x4E00;&#x6A21;&#x4E00;&#x6837;&#xFF0C;&#x90FD;&#x8BF4;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<shape>&#x6807;&#x7B7E;&#x5728;xml&#x6587;&#x4EF6;&#x4E2D;&#x5B9A;&#x4E49;&#x3002; </shape></li>
<li>&#x90A3;&#x4E48;&#xFF0C;&#x5230;&#x5E95;&#x7528;<shape>&#x6807;&#x7B7E;&#x5B9A;&#x4E49;&#x7684;&#x662F;&#x4EC0;&#x4E48;&#x7684;&#x5462;&#xFF1F;</shape></li>
<li>&#x7ECF;&#x8FC7;&#x4E0B;&#x9762;&#x7684;&#x5B9E;&#x9A8C;&#xFF1A;</li>
<li>TextView textView=(TextView)findViewById(R.id.textView);</li>
<li>ShapeDrawable gradientDrawable=(ShapeDrawable)textView.getBackground();</li>
<li>&#x62A5;&#x9519;&#xFF0C;&#x7C7B;&#x578B;&#x8F6C;&#x6362;&#x9519;&#x8BEF;java.lang.ClassCastException:android.graphics.drawable.GradientDrawable </li>
<li>&#x6240;&#x4EE5;&#x662F;GradientDrawable</li>
<li>&#x90A3;&#x4E48;&#xFF0C;ShapeDrawable&#x662F;&#x600E;&#x4E48;&#x5B9A;&#x4E49;&#x7684;&#xFF0C;&#x627E;&#x4E86;&#x7F51;&#x4E0A;&#x7684;&#x8D44;&#x6599;&#xFF0C;&#x7ED3;&#x679C;&#x786C;&#x662F;&#x6CA1;&#x627E;&#x5230;&#x5982;&#x4F55;&#x5728;XML&#x6587;&#x4EF6;&#x4E2D;&#x5B9A;&#x4E49;&#x5B83;&#xFF0C;&#x53EA;&#x80FD;&#x901A;&#x8FC7;&#x4EE3;&#x7801;&#x7684;&#x65B9;&#x5F0F;&#x5B9E;&#x73B0;</li>
</ul>
</li>
</ul>
<p><strong>xml&#x89C4;&#x683C;</strong></p>
<pre><code class="lang-xml"><span class="php"><span class="hljs-meta">&lt;?</span>xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;utf-8&quot;</span><span class="hljs-meta">?&gt;</span></span>
<span class="hljs-tag">&lt;<span class="hljs-name">shape</span>
    <span class="hljs-attr">xmlns:android</span>=<span class="hljs-string">&quot;http://schemas.android.com/apk/res/android&quot;</span>
    <span class="hljs-attr">android:shape</span>=<span class="hljs-string">[&quot;rectangle&quot;</span> | &quot;<span class="hljs-attr">oval</span>&quot; | &quot;<span class="hljs-attr">line</span>&quot; | &quot;<span class="hljs-attr">ring</span>&quot;] &gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">corners</span>
        <span class="hljs-attr">android:radius</span>=<span class="hljs-string">&quot;integer&quot;</span>
        <span class="hljs-attr">android:topLeftRadius</span>=<span class="hljs-string">&quot;integer&quot;</span>
        <span class="hljs-attr">android:topRightRadius</span>=<span class="hljs-string">&quot;integer&quot;</span>
        <span class="hljs-attr">android:bottomLeftRadius</span>=<span class="hljs-string">&quot;integer&quot;</span>
        <span class="hljs-attr">android:bottomRightRadius</span>=<span class="hljs-string">&quot;integer&quot;</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">gradient</span>
        <span class="hljs-attr">android:angle</span>=<span class="hljs-string">&quot;integer&quot;</span>
        <span class="hljs-attr">android:centerX</span>=<span class="hljs-string">&quot;integer&quot;</span>
        <span class="hljs-attr">android:centerY</span>=<span class="hljs-string">&quot;integer&quot;</span>
        <span class="hljs-attr">android:centerColor</span>=<span class="hljs-string">&quot;integer&quot;</span>
        <span class="hljs-attr">android:endColor</span>=<span class="hljs-string">&quot;color&quot;</span>
        <span class="hljs-attr">android:gradientRadius</span>=<span class="hljs-string">&quot;integer&quot;</span>
        <span class="hljs-attr">android:startColor</span>=<span class="hljs-string">&quot;color&quot;</span>
        <span class="hljs-attr">android:type</span>=<span class="hljs-string">[&quot;linear&quot;</span> | &quot;<span class="hljs-attr">radial</span>&quot; | &quot;<span class="hljs-attr">sweep</span>&quot;]
        <span class="hljs-attr">android:useLevel</span>=<span class="hljs-string">[&quot;true&quot;</span> | &quot;<span class="hljs-attr">false</span>&quot;] /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">padding</span>
        <span class="hljs-attr">android:left</span>=<span class="hljs-string">&quot;integer&quot;</span>
        <span class="hljs-attr">android:top</span>=<span class="hljs-string">&quot;integer&quot;</span>
        <span class="hljs-attr">android:right</span>=<span class="hljs-string">&quot;integer&quot;</span>
        <span class="hljs-attr">android:bottom</span>=<span class="hljs-string">&quot;integer&quot;</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">size</span>
        <span class="hljs-attr">android:width</span>=<span class="hljs-string">&quot;integer&quot;</span>
        <span class="hljs-attr">android:height</span>=<span class="hljs-string">&quot;integer&quot;</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">solid</span>
        <span class="hljs-attr">android:color</span>=<span class="hljs-string">&quot;color&quot;</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">stroke</span>
        <span class="hljs-attr">android:width</span>=<span class="hljs-string">&quot;integer&quot;</span>
        <span class="hljs-attr">android:color</span>=<span class="hljs-string">&quot;color&quot;</span>
        <span class="hljs-attr">android:dashWidth</span>=<span class="hljs-string">&quot;integer&quot;</span>
        <span class="hljs-attr">android:dashGap</span>=<span class="hljs-string">&quot;integer&quot;</span> /&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">shape</span>&gt;</span>
</code></pre>
<p><strong>&#x89E3;&#x91CA;</strong><br>    ----------------<gradient>&#x6307;&#x5B9A;&#x8FD9;&#x4E2A;shape&#x7684;&#x6E10;&#x53D8;&#x989C;&#x8272;
    android:angle  &#x6E10;&#x53D8;&#x7684;&#x89D2;&#x5EA6;&#x3002; 0 &#x4EE3;&#x8868;&#x4ECE; left &#x5230; right&#x3002;90 &#x4EE3;&#x8868;bottom&#x5230; top&#x3002;&#x5FC5;&#x987B;&#x662F;45&#x7684;&#x500D;&#x6570;&#xFF0C;&#x9ED8;&#x8BA4;&#x4E3A;0
    android:centerX  &#x6E10;&#x53D8;&#x4E2D;&#x5FC3;&#x7684;&#x76F8;&#x5BF9;X&#x5750;&#x6807;&#xFF0C;&#x5728;0&#x5230;1.0&#x4E4B;&#x95F4;&#x3002;
    android:centerY  &#x6E10;&#x53D8;&#x4E2D;&#x5FC3;&#x7684;&#x76F8;&#x5BF9;Y&#x5750;&#x6807;&#xFF0C;&#x5728;0&#x5230;1.0&#x4E4B;&#x95F4;&#x3002;
    android:centerColor  &#x53EF;&#x9009;&#x7684;&#x989C;&#x8272;&#x503C;&#x3002;&#x57FA;&#x4E8E;startColor&#x548C;endColor&#x4E4B;&#x95F4;
    android:endColor  &#x7ED3;&#x675F;&#x7684;&#x989C;&#x8272;
    android:gradientRadius  &#x6E10;&#x53D8;&#x7684;&#x534A;&#x5F84;&#x3002;&#x53EA;&#x6709;&#x5728; android:type=&quot;radial&quot;&#x624D;&#x4F7F;&#x7528;
    android:startColor  &#x5F00;&#x59CB;&#x7684;&#x989C;&#x8272;&#x503C;&#x3002;
    android:type &#x6E10;&#x53D8;&#x7684;&#x6A21;&#x5F0F;&#xFF0C;&#x4E0B;&#x9762;&#x503C;&#x4E4B;&#x4E00;&#xFF1A;
    &quot;linear&quot;    &#x7EBF;&#x5F62;&#x6E10;&#x53D8;&#x3002;&#x8FD9;&#x4E5F;&#x662F;&#x9ED8;&#x8BA4;&#x7684;&#x6A21;&#x5F0F;
    &quot;radial&quot;    &#x8F90;&#x5C04;&#x6E10;&#x53D8;&#x3002;startColor&#x5373;&#x8F90;&#x5C04;&#x4E2D;&#x5FC3;&#x7684;&#x989C;&#x8272;
    &quot;sweep&quot;    &#x626B;&#x63CF;&#x7EBF;&#x6E10;&#x53D8;&#x3002;
    android:useLevel  &#x5982;&#x679C;&#x5728;LevelListDrawable&#x4E2D;&#x4F7F;&#x7528;&#xFF0C;&#x5219;&#x4E3A;true</gradient></p>
<pre><code>----------------&lt;padding&gt;&#x5185;&#x5BB9;&#x4E0E;&#x89C6;&#x56FE;&#x8FB9;&#x754C;&#x7684;&#x8DDD;&#x79BB;
android:left &#x5DE6;&#x8FB9;&#x586B;&#x5145;&#x8DDD;&#x79BB;.
android:top &#x9876;&#x90E8;&#x586B;&#x5145;&#x8DDD;&#x79BB;.
android:right  &#x53F3;&#x8FB9;&#x586B;&#x5145;&#x8DDD;&#x79BB;.
android:bottom &#x5E95;&#x90E8;&#x586B;&#x5145;&#x8DDD;&#x79BB;.

----------------&lt;size&gt;&#x8FD9;&#x4E2A;shape&#x7684;&#x5927;&#x5C0F;
android:height  &#x8FD9;&#x4E2A;shape&#x7684;&#x9AD8;&#x5EA6;&#x3002;
android:width  &#x8FD9;&#x4E2A;shape&#x7684;&#x5BBD;&#x5EA6;&#x3002;
&#x6CE8;&#x610F;&#xFF1A;&#x9ED8;&#x8BA4;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x8FD9;&#x4E2A;shape&#x4F1A;&#x7F29;&#x653E;&#x5230;&#x4E0E;&#x4ED6;&#x6240;&#x5728;&#x5BB9;&#x5668;&#x5927;&#x5C0F;&#x6210;&#x6B63;&#x6BD4;&#x3002;
&#x5F53;&#x4F60;&#x5728;&#x4E00;&#x4E2A;ImageView&#x4E2D;&#x4F7F;&#x7528;&#x8FD9;&#x4E2A;shape&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; android:scaleType=&quot;center&quot;&#x6765;&#x9650;&#x5236;&#x8FD9;&#x79CD;&#x7F29;&#x653E;&#x3002;

----------------&lt;solid&gt;&#x586B;&#x5145;&#x8FD9;&#x4E2A;shape&#x7684;&#x7EAF;&#x8272;
android:color  &#x989C;&#x8272;&#x503C;&#xFF0C;&#x5341;&#x516D;&#x8FDB;&#x5236;&#x6570;&#xFF0C;&#x6216;&#x8005;&#x4E00;&#x4E2A;Color&#x8D44;&#x6E90;

----------------&lt;stroke&gt; &#x8FD9;&#x4E2A;shape&#x4F7F;&#x7528;&#x7684;&#x7B14;&#x753B;&#xFF0C;&#x5F53;android:shape=&quot;line&quot;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x5FC5;&#x987B;&#x8BBE;&#x7F6E;&#x6539;&#x5143;&#x7D20;&#x3002;
android:width &#x7B14;&#x753B;&#x7684;&#x7C97;&#x7EC6;&#x3002;
android:color &#x7B14;&#x753B;&#x7684;&#x989C;&#x8272;
android:dashGap &#x6BCF;&#x753B;&#x4E00;&#x6761;&#x7EBF;&#x5C31;&#x95F4;&#x9694;&#x591A;&#x5C11;&#x3002;&#x53EA;&#x6709;&#x5F53;android:dashWidth&#x4E5F;&#x8BBE;&#x7F6E;&#x4E86;&#x624D;&#x6709;&#x6548;&#x3002;
android:dashWidth &#x6BCF;&#x753B;&#x4E00;&#x6761;&#x7EBF;&#x7684;&#x957F;&#x5EA6;&#x3002;&#x53EA;&#x6709;&#x5F53; android:dashGap&#x4E5F;&#x8BBE;&#x7F6E;&#x4E86;&#x624D;&#x6709;&#x6548;&#x3002;
</code></pre><h2 id="2-&#x81EA;&#x5B9A;&#x4E49;drawable">2 &#x81EA;&#x5B9A;&#x4E49;Drawable</h2>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="textview.html" class="navigation navigation-prev " aria-label="Previous page: TextView">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="item-template.html" class="navigation navigation-next " aria-label="Next page: 列表Adapter模板化">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"Drawable","level":"1.11","depth":1,"next":{"title":"列表Adapter模板化","level":"1.12","depth":1,"path":"item-template.md","ref":"item-template.md","articles":[]},"previous":{"title":"TextView","level":"1.10","depth":1,"path":"textview.md","ref":"textview.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"drawable.md","mtime":"2017-04-09T15:00:43.184Z","type":"markdown"},"gitbook":{"version":"3.2.2","time":"2017-04-09T17:03:25.521Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

